package com.songshuai.pangu.api.component;

import com.alibaba.fastjson.JSONObject;
import com.songshuai.pangu.api.controller.BaseController;
import com.songshuai.pangu.api.dto.pangu.PictureResDto;
import com.songshuai.webapi.baseapi.dto.feign.request.custminiprogram.AddMeetingReceiptReqDto;
import com.songshuai.webapi.baseapi.dto.feign.request.custminiprogram.MeetingDetailReqDto;
import com.songshuai.webapi.baseapi.dto.feign.request.custminiprogram.MeetingReceiptReqDto;
import com.songshuai.webapi.baseapi.dto.feign.request.custminiprogram.WxQRCodeReqDto;
import com.songshuai.webapi.baseapi.enumerations.cust.AttendStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import java.util.stream.Collectors;

/**
 * 对接盘古-客户活动
 * @Author: wbl
 * @Date: 2019/3/29
 */
@Slf4j
@Component
public class CustMeetingComponent extends BaseController {

    //根据客户id获取会议列表
    @Value("${pangu.cust.getMeetingListByUserId}")
    private String getMeetingListByUserIdUrl;

    //根据邀约编号获取回执单
    @Value("${pangu.cust.queryReceipt}")
    private String queryReceiptUrl;

    //根据邀约编号获取会议详情
    @Value("${pangu.cust.getMeetingDetail}")
    private String getMeetingDetailUrl;

    //根据会议编号获取邀约模板
    @Value("${pangu.cust.getInvitationTemplateByCode}")
    private String getInvitationTemplateByCodeUrl;

    //根据线索id和会议编号获取签到码
    @Value("${pangu.cust.getWxQRCode}")
    private String getWxQRCodeUrl;

    //根据线索id和会议编号获取签到码
    @Value("${pangu.cust.addMeetingReceipt}")
    private String addMeetingReceiptUrl;

    //根据会议编号获取车站信息
    @Value("${pangu.cust.getAreaStations}")
    private String getAreaStationsUrl;

    @Value("${pangu.cust.mySale}")
    private String mySaleUrl;

    //根据客户id获取会议列表
    public String getMeetingListByUserId(String customId) {
        try {
            JSONObject object = new JSONObject();
            object.put("customGuid", Long.parseLong(customId));
            return callPanguApi(object, getMeetingListByUserIdUrl);
        } catch (Exception e) {
            log.error("请求盘古-获取会议列表", e);
        }
        return null;
    }

    @Value("${pangu.cust.historyMeetings}")
    private String historyMeetingsUrl;


    //根据邀约编号获取回执单
    public String queryReceipt(MeetingReceiptReqDto parm) {
        try {
            JSONObject object = new JSONObject();
            object.put("inviteNo", parm.getInviteCode());
            return callPanguApi(object, queryReceiptUrl);
        } catch (Exception e) {
            log.error("请求盘古-获取回执单", e);
        }
        return null;
    }

    //根据邀约编号获取会议详情
    public String getMeetingDetail(MeetingDetailReqDto parm) {
        try {
            JSONObject object = new JSONObject();
            object.put("inviteNo", parm.getInviteCode());
            return callPanguApi(object, getMeetingDetailUrl);
        } catch (Exception e) {
            log.error("请求盘古-获取会议详情", e);
        }
        return null;
    }

    //根据会议编号获取邀约模板
    public String getInvitationTemplateByCode(String meetingCode) {
        try {
            JSONObject object = new JSONObject();
            object.put("meetingCode", meetingCode);
            return callPanguApi(object, getInvitationTemplateByCodeUrl);
        } catch (Exception e) {
            log.error("请求盘古-获取邀约模板", e);
        }
        return null;
    }

    //根据线索id和会议编号获取签到码
    public String getWxQRCode(WxQRCodeReqDto parm) {
        try {
            JSONObject object = new JSONObject();
            object.put("clueGuid", parm.getClueGuid());
            object.put("meetingNo", parm.getMeetingCode());
            return callPanguApi(object, getWxQRCodeUrl);
        } catch (Exception e) {
            log.error("请求盘古-获取签到码", e);
        }
        return null;
    }

    //根据邀约编号获取会议详情
    public String getMySale(String userId) {
        try {
            Map object = new HashMap();
            object.put("CustomGuid", userId);
            return getCallPanguApi(object, mySaleUrl);
        } catch (Exception e) {
            log.error("请求盘古-获取会议详情", e);
        }
        return null;
    }

    //新增回执单
    public String addMeetingReceipt(AddMeetingReceiptReqDto parm) {
        try {
            log.info("请求发送新增回执单请求参数开始，入住人姓名：{},备注：{}", parm.getTenants(), parm.getBack());
            JSONObject object = new JSONObject();
            object.put("Receipt_No",parm.getReceiptNo());
            object.put("Meeting_No",parm.getMeetingNo());
            object.put("Clueguid",parm.getClueGuid());
            object.put("invite_no",parm.getInviteNo());
            object.put("CompanionNum",parm.getCompanionNum());
            object.put("IsNeedRoom",parm.getIsNeedRoom());
            object.put("RoomStyle",parm.getRoomStyle());
            object.put("Tenants",parm.getTenants());
            object.put("TripMode",parm.getTripMode());
            object.put("FlightOrTrainNum",parm.getFlightOrTrainNum());
            object.put("IsFullAttend",parm.getIsFullAttend());
            object.put("RoomCnt",parm.getRoomCnt());
            object.put("IsNeedMeet",parm.getIsNeedMeet());
            object.put("Station",parm.getStation());
            object.put("ArriveTime",parm.getArriveTime());
            object.put("Back",parm.getBack());
            object.put("PicList",parm.getPicList().stream().map(x -> {
                return new PictureResDto(){{
                   setName(x.getPicName());
                   setAddress(x.getPicAddress());
                   setPicSort(x.getPicSort());
                }};
            }).collect(Collectors.toList()));
            log.info("请求发送新增回执单请求参数，入住人姓名：{},备注：{}", object.getString("Tenants"), object.getString("Back"));
            return callPanguApi(object, addMeetingReceiptUrl);
        } catch (Exception e) {
            log.error("请求盘古-新增回执单", e);
        }
        return null;
    }

    //根据会议编号获取车站信息
    public String getAreaStations(String meetingCode) {
        try {
            JSONObject object = new JSONObject();
            object.put("MeetingNo", meetingCode);
            return callPanguApi(object, getAreaStationsUrl);
        } catch (Exception e) {
            log.error("请求盘古-获取车站信息", e);
        }
        return null;
    }
    // 查询我参与过的活动列表(出席状态为)
    public String queryHistoryActivityList(String customGuid){
        JSONObject object = new JSONObject();
        object.put("customGuid", customGuid);
        Integer[] attendStatus = new Integer[]{AttendStatusEnum.MeetAttend.getCode(), AttendStatusEnum.MeetLeave.getCode()};
        object.put("attendStatus", attendStatus);
        return callPanguApi(object, historyMeetingsUrl);
    }
}